package com.tendory.common.per;

import android.Manifest;
import android.app.Activity;
import android.content.Context;

import androidx.appcompat.app.AlertDialog;

public class PermissionCusRent extends Permission {

    private class Permissions {
        String permission;
        String rationale;
        int result;
        public Permissions(String p, String r) {
            permission = p;
            rationale = r;
        }
    }
    private Permissions[] ps = new Permissions[] {
            new Permissions(Manifest.permission.ACCESS_COARSE_LOCATION, "GPS"),
            new Permissions(Manifest.permission.ACCESS_FINE_LOCATION, "GPS"),
            new Permissions(Manifest.permission.READ_CONTACTS, "联系人"),
            new Permissions(Manifest.permission.WRITE_CONTACTS, "联系人"),
            new Permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, "手机存储"),
            new Permissions(Manifest.permission.WRITE_CALL_LOG, "通话记录"),
            new Permissions(Manifest.permission.WRITE_CALENDAR, "日程"),
            new Permissions(Manifest.permission.RECORD_AUDIO, "录音"),
            new Permissions(Manifest.permission.READ_SMS, "短信"),
            new Permissions(Manifest.permission.RECEIVE_SMS, "收短信"),
//            new Permissions(Manifest.permission.CHANGE_WIFI_STATE, LanguageUtil.unify(WeShareApplication.getInstance(), R.string.zas_permission_wifi_st)),
            new Permissions(Manifest.permission.CAMERA, "照像机"),
            //either of
            new Permissions(Manifest.permission.READ_PHONE_STATE, "读手机状态"),
            // new Permissions(Manifest.permission.CHANGE_NETWORK_STATE, "Change network st"),
    };

    public PermissionCusRent(Object o) {
        super(o);
    }

    private String showRationalContent(Context context, String[] perms) {
        String content = "你需要取得以下权限:" + "\n";
        for (String p : perms) {
            Permissions per = find(p);
            if (per != null) {
                content = content + "\n" + per.rationale;
            } else {
                content = content + "\n" + p;
            }
        }
        return content;
    }

    private Permissions find(String p) {
        for (Permissions per : ps) {
            if (per.permission.equals(p)) {
                return per;
            }
        }
        return null;
    }

    @Override
    protected void onShowRationaleDialog(final Activity activity, final Object o, final String[] perms, final int requestCode) {
        String content = showRationalContent(activity, perms);
        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setTitle("权限获取提示")
                .setMessage(content)
                .setPositiveButton("确认", (dialog1, which) -> PermissionCusRent.super.onShowRationaleDialog(activity, o, perms, requestCode))
                .setNegativeButton("退出", (dialog12, which) -> permissionsDenied()).create();
        dialog.show();
    }

    @Override
    protected boolean onShowRationaleDialog2(final Activity activity, final String[] perms) {
        String content = showRationalContent(activity, perms);
        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setTitle("权限获取提示")
                .setMessage(content)
                .setPositiveButton("去设置", (dialog1, which) -> PermissionCusRent.super.onShowRationaleDialog2(activity, perms))
                .setNegativeButton("退出", (dialogInterface, i) -> permissionsDenied())
                .create();
        dialog.show();
        return true;
    }
}
